package com.frogs.ucenter.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.AreaService;

@Service("areaService")
public class AreaServiceImpl extends AbsService implements AreaService {
	
	private static Logger log = LoggerFactory.getLogger(AreaServiceImpl.class);

	@Override
	public JSONObject getAreaInfo(String province,String city, String district) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("AREA_INFO_SHOW");
		String[] otherParams = {ApiUrlParamEnum.province.getParamName(),province,
				ApiUrlParamEnum.city.getParamName(),city,
				ApiUrlParamEnum.district.getParamName(),district};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result.getJSONObject("response").getJSONObject("info");
		} catch (Exception e) {
			log.error("func[getAreaInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(), Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String,Object>> getSubAreaList(String parentId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("AREA_INFO_SUBLIST");
		String[] otherParams = {ApiUrlParamEnum.parentId.getParamName(),parentId};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[getSubAreaList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(), Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return JSONObject.toJavaObject(result.getJSONObject("response").getJSONArray("list"), List.class);
	}
	
	@Override
	public JSONObject showPostCode(String address, String areaId) throws Exception {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("POST_CODE_SHOW");
		String[] otherParams = {ApiUrlParamEnum.address.getParamName(),address,
				ApiUrlParamEnum.areaCode.getParamName(),areaId};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result;
		} catch (IllegalStateException e) {
			log.error("func[showPostCode] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(), Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		} catch (Exception e) {
			throw new Exception("获取邮编信息接口失败！");
		}
		return null;
	}


}
